﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using r4u.business.logic.layer.RECRUIT;
using r4u.data.access.layer;

namespace r4u.Controllers
{
    public class PublishController : Controller
    {
        //
        // GET: /Publish/

        public ActionResult Index(int id)
        {
            if (User.IsInRole("TRIAL"))
            {
                Vacancy temp = new Vacancy();
                VacancyObject[] vacancies = temp.GetVacancies(HttpContext.User.Identity.Name);
                vacancies = vacancies.Where(v => v.status.Equals("PUBLISHED")).ToArray();

                if (vacancies.Length > 0)
                {
                    ViewBag.error = "Trial users can only publish one job vacancy. Please upgrade to premium.";
                    return View("Error");
                }

            }


            using (var context = new r4uEntities())
            {
                VACANCY v = context.VACANCies.FirstOrDefault(x => x.vacancy_id.Equals(id.ToString()));
                v.status = "PUBLISHED";
                context.SaveChanges();
            }

            return RedirectToAction("Index", "Apply", new { id = id });
        }

    }
}
